<template>
  <div class="app-container">
    <el-form :model="queryParam" ref="queryForm" :inline="true">
      <el-form-item label="试卷ID：">
        <el-input v-model="queryParam.id" clearable></el-input>
      </el-form-item>
      <el-form-item label="年级：">
        <el-select
          v-model="queryParam.level"
          placeholder="年级"
          @change="levelChange"
          clearable>
          <el-option
            v-for="item in levelEnum"
            :key="item.key"
            :value="item.key"
            :label="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="课程：">
        <el-select v-model="queryParam.subjectId" clearable placeholder="请先选择年级">
          <el-option
            v-for="item in subjectFilter"
            :key="item.id"
            :value="item.id"
            :label="item.name + ' ( ' + item.levelName + ' )'"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">查询</el-button>
        <router-link :to="{ path: '/exam/paper/edit' }" class="link-left">
          <el-button type="primary">添加</el-button>
        </router-link>
        <router-link :to="{ path: '/exam/paper/autocreate' }" class="link-left">
          <el-button type="primary">自动组卷</el-button>
        </router-link>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="listLoading"
      :data="tableData"
      border
      fit
      highlight-current-row
      style="width: 100%">
      <el-table-column prop="id" label="Id" width="90px" align="center" />
      <el-table-column
        prop="subjectId"
        label="课程"
        :formatter="subjectFormatter"
        align="center"/>
      <el-table-column prop="name" label="名称" align="center" />
      <!--<el-table-column prop="classname" label="班级" width="150px" align="center" />-->
      <el-table-column prop="questionCount" label="题量" width="150px" align="center"/>
      <el-table-column prop="score" label="总分" width="150px" align="center" />
      <!--<el-table-column prop="createUser" label="创建人id" width="150px" align="center" />-->
      <el-table-column
        prop="createTime"
        label="创建时间"
        align="center"/>
      <el-table-column label="操作" align="center" width="307px" fixed="right">
        <template slot-scope="{ row }">
          <el-button
            size="mini"
            @click="$router.push({ path: '/exam/paper/edit', query: { id: row.id } })">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="deletePaper(row)"
            class="link-left">删除</el-button>
          <router-link :to="{ path: `/exam/paper/preview?id=${row.id}` }" class="link-left">
            <el-button size="mini">预览</el-button>
          </router-link>
<!--          <router-link :to="{ path: `/task/manage/publish?id=${row.id}&name=${row.name}` }" class="link-left">-->
<!--            <el-button size="mini">发布考试</el-button>-->
<!--          </router-link>-->
          <el-button
            size="mini"
            @click="$router.push({ path: '/task/manage/publish', query: { paperid: row.id,name:row.name } })"
            style="margin-left: 5px">发布考试</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParam.pageIndex"
      :limit.sync="queryParam.pageSize"
      @pagination="search"/>
  </div>
</template>

<script>
import { mapGetters, mapState, mapActions } from 'vuex'
import Pagination from '@/components/Pagination'
import examPaperApi from '@/api/examPaper'

export default {
  components: { Pagination },
  data () {
    return {
      queryParam: {
        id: null,
        level: null,
        subjectId: null,
        pageIndex: 1,
        pageSize: 10
      },
      subjectFilter: null,
      listLoading: true,
      tableData: [],
      total: 0
    }
  },
  created () {
    this.initSubject()
    this.search()
    console.log('this.subjects',this.subjects)
  },
  methods: {
    submitForm () {
      this.queryParam.pageIndex = 1
      this.search()
    },
    search () {
      this.listLoading = true
      examPaperApi.pageList(this.queryParam).then((data) => {
        const re = data.response
        console.log('【试卷列表 examPaperApi.pageList】',re)
        re.list.map(item => {
          item.score = item.score / 10
        })
        this.tableData = re.list
        this.total = re.total
        this.queryParam.pageIndex = re.pageNum
        this.listLoading = false
      })
    },
    deletePaper (row) {
      let _this = this
      examPaperApi.deletePaper(row.id).then((re) => {
        if (re.code === 1) {
          _this.search()
          _this.$message.success(re.message)
        } else {
          _this.$message.error(re.message)
        }
      })
    },
    levelChange () {
      this.queryParam.subjectId = null
      this.subjectFilter = this.subjects.filter(
        (data) => data.level === this.queryParam.level
      )
      console.log('this.subjectFilter',this.subjectFilter)
    },
    subjectFormatter (row, column, cellValue, index) {
      return this.subjectEnumFormat(cellValue)
    },
    ...mapActions('exam', { initSubject: 'initSubject' })
  },
  computed: {
    ...mapGetters('enumItem', ['enumFormat']),
    ...mapState('enumItem', {
      levelEnum: (state) => state.user.levelEnum
    }),
    ...mapGetters('exam', ['subjectEnumFormat']),
    ...mapState('exam', { subjects: (state) => state.subjects })
  }
}
</script>
